﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace KinectProject
{
    /// <summary>
    /// Interaction logic for statsPage.xaml
    /// </summary>
    public partial class parameterPage : Page
    {
        

        //trial pattern table (1 or more trials)
        VRGETTableAdapters.TrialPatternAdapter tpAdapter;
        VRGET.TrialPatternDataTable tpDataTable;
        VRGET.TrialPatternRow tpRow;

        //individual trials (1 trial)
        VRGETTableAdapters.TrialAdapter tAdapter;
        VRGET.TrialDataTable tDataTable;
        VRGET.TrialRow tRow;


        //special display table
        VRGETTableAdapters.TrialPatternsAdapter tpsAdapter;
        VRGET.TrialPatternsDataTable DataTableDisplay;

        dbController db;
        int i = 0;

        public parameterPage()
        {
            InitializeComponent();
            tAdapter   = new VRGETTableAdapters.TrialAdapter();
            tpAdapter  = new VRGETTableAdapters.TrialPatternAdapter();
            tpsAdapter = new VRGETTableAdapters.TrialPatternsAdapter();

            tDataTable       = new VRGET.TrialDataTable();
            tpDataTable      = new VRGET.TrialPatternDataTable();
            DataTableDisplay = new VRGET.TrialPatternsDataTable();

            //reset 
            labelSpeed.Content = "";
            labelWeight.Content = "";
            labelFlexion.Content = "";


            /*
            // Create OpenFileDialog
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();


            // Set filter for file extension and default file extension
            dlg.FileName = "Document"; // Default file name
            dlg.DefaultExt = ".txt";
            dlg.Filter = "Text documents (.txt)|*.txt";

            // Display OpenFileDialog by calling ShowDialog method
            Nullable<bool> result = dlg.ShowDialog();

            // Get the selected file name and display in a TextBox
            if (result == true)
            {
                // Open document
                string filename = dlg.FileName;
                //FileNameTextBox.Text = filename;
            }

            Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog();
            dialog.Filter = "CSV Files (*.csv)|*.csv";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                System.Data.DataTable table = CSVReader.ReadCSVFile(dialog.FileName, true);
                dataGrid1.ItemsSource = table;
            }

            */

        }



        private void insertButton_Click(object sender, RoutedEventArgs e)
        {

            //validate textboxes.text
            if (labelSpeed.Content == "" || labelWeight.Content == "" || labelFlexion.Content == "")
            {
                MessageBox.Show("Please enter all values");
                return;
            }

            //Insert values into trial pattern
            //-----------------------------------------------
            string s_id;

            log.speed = labelSpeed.Content+"";
            log.weight = labelWeight.Content+"";
            log.flexion = labelFlexion.Content+"";

           
            //if no current trial pattern...
            //new trial pattern row
            if (tpRow == null)
            {
                i = 1;
                tpDataTable = tpAdapter.GetTrialPattern();
                tpRow       = (VRGET.TrialPatternRow)tpDataTable.NewRow();

                //find and set correct tp_id for new row
                //-------------------------------------------------
                s_id = tpDataTable[tpDataTable.Rows.Count - 1].tp_id;
                int i_id;
                int.TryParse(s_id, out i_id);
                if (i_id !=  0)
                {

                    i_id++;
                    tpRow.tp_id = "" + i_id;
                    log.trialPattern = i_id; //save tp_id for global access
                }
                else
                {
                    MessageBox.Show("ERROR setting Trial pattern ID");
                }

                tpDataTable.Rows.Add(tpRow);

            }




            //Get trial number by values
            tDataTable = tAdapter.GetTrialByValues(log.speed,log.weight, log.flexion);
            tRow       = tDataTable[0];

           
            //Insert trial number into Trial pattern row
            if (i < 20)
            {
                tpRow["t"+i] = tRow.t_id;
                i++;
            }
            else
            {
                MessageBox.Show("Max 20  trials");
            }

            tpAdapter.Update(tpDataTable);

            DataTableDisplay.Clear();
            DataTableDisplay      = tpsAdapter.GetByPatternId(tpRow.tp_id);
            dataGrid.ItemsSource = DataTableDisplay;
                
           // dataGrid1.Items.Refresh();

            //reset 
            labelSpeed.Content = "";
            labelWeight.Content = "";
            labelFlexion.Content = "";



            

        }

        private void button2_Click(object sender, RoutedEventArgs e){labelFlexion.Content = "30";}
        private void button3_Click(object sender, RoutedEventArgs e){labelFlexion.Content = "60";}
        private void button4_Click(object sender, RoutedEventArgs e){labelFlexion.Content = "80";}
        private void button5_Click(object sender, RoutedEventArgs e){labelFlexion.Content = "90";}
        private void button6_Click(object sender, RoutedEventArgs e){ labelSpeed.Content = "Fast";}
        private void button7_Click(object sender, RoutedEventArgs e){labelSpeed.Content = "Regular"; }
        private void button8_Click(object sender, RoutedEventArgs e){labelWeight.Content = "None";}
        private void button9_Click(object sender, RoutedEventArgs e){labelWeight.Content =  "2kg";}

        private void startButton_Click(object sender, RoutedEventArgs e){



            //If no trials in Trial pattern... throw error
            if (tpRow == null)
            {
                MessageBoxResult result = MessageBox.Show("No trials to start");               
            }
            else {

                if (textBoxFName.Text == "" || textBoxLName.Text == "")
                {
                    MessageBox.Show("Please enter First and Last name");

                }
                else
                {

                    log.lName = textBoxLName.Text;
                    log.fName = textBoxFName.Text;

                    //var skelwindow = new SkeletalTrackingWindow(textBoxFName.Text, textBoxLName.Text, (labelSpeed.Content+""), textBoxWeight.Text, textBoxFlexion.Text);
                    //var skelwindow = new SkeletalTrackingWindow();
                    //skelwindow.Show();

                    var window = new DisplayPage();
                    window.Show();

                    //Disable Inputs when running trials?
                }
            }
        }

        static Random _r = new Random();
        bool first = true;
        private void debugButton_Click(object sender, RoutedEventArgs e)
        {
            if (first)
            {

                first = false;
                if (textBoxFName.Text == "" || textBoxLName.Text == "")
                {
                    MessageBox.Show("Please enter First and Last name");

                }
                else
                {

                    log.lName = textBoxLName.Text;
                    log.fName = textBoxFName.Text;
                    //for testing only....
                    db = new dbController();
                    //-----------------------------

                    while (log.moreTrials)
                    { 
                        //generate random results 
                        log.anxiety = _r.Next(10);
                        log.harm = _r.Next(10);
                        log.pain = _r.Next(10);
                        log.epain = _r.Next(10);


                        db.saveResults();

                        db.getNextValues();
                        labelSpeed.Content = log.speed;
                        labelWeight.Content = log.weight;
                        labelFlexion.Content = log.flexion;
                        //Disable Inputs when running trials?
                    }
                    MessageBox.Show("Done.");
                    db.close();
                }
            }
            
        }

        private void Grid_Unloaded(object sender, RoutedEventArgs e)
        {
           // MessageBox.Show("unloaded");
        }




  

    }


}
